package com.bjqg.bjqg01.bjqg.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ajguan.library.EasyRefreshLayout;
import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.NoticeAdapter;
import com.bjqg.bjqg01.bjqg.bean.Client;
import com.bjqg.bjqg01.bjqg.bean.NoticeBean;
import com.bjqg.bjqg01.bjqg.utils.Constant;
import com.bjqg.bjqg01.bjqg.utils.GreendaoUtils;
import com.bjqg.greendao.ClientDao;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by bjqg01 on 2017/12/12.
 */

public class NoticeActivity extends BaseActivity implements View.OnClickListener {

    private String yema = "1";
    private ImageView goback;
    private TextView toptitle;
    private ImageView select;
    private ImageView broadimg;
    private RecyclerView rvNotice;
    private NoticeAdapter noticeAdapter;
    private ArrayList<String> list;
    private String[] split1;
    private ArrayList<NoticeBean.DsBean> infoBean;
    private int msgwhat = 0;
    private EasyRefreshLayout noticeRefresh;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        initView();
    }

    private void initView() {
        goback = (ImageView) findViewById(R.id.back);
        toptitle = (TextView) findViewById(R.id.tv_top_title);
        select = (ImageView) findViewById(R.id.select);

        rvNotice = (RecyclerView) findViewById(R.id.rv_notice);
        noticeRefresh = (EasyRefreshLayout) findViewById(R.id.notice_refresh_layout);

        goback.setOnClickListener(this);

        toptitle.setText("公告");
        select.setVisibility(View.INVISIBLE);

        initData();


    }

    private void initData() {

        getokgo(yema);
//        getokgo("4");

    }

    private boolean isLoad;
    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    NoticeBean refreshinfo = msg.getData().getParcelable("noticeBean");
                    yema = refreshinfo.getPageNum().toString();
                    int size = refreshinfo.getDs().size();
                    if (size < 15){
                        isLoad = true;
                    }else {
                        isLoad = false;
                    }

                    showListView(refreshinfo);
                    break;
                case 1:
                    NoticeBean loadinfo = msg.getData().getParcelable("noticeBean");
                    yema = loadinfo.getPageNum().toString();
                    List<NoticeBean.DsBean> ds = loadinfo.getDs();
                    if (ds.size() < 15){
                        isLoad = true;
                    }
                    infoBean.addAll(ds);
                    noticeAdapter.setNewData(infoBean);
//                    rvplu.notify();
                    Log.e("----------handler","失败：------------------------okgo"+loadinfo.toString());
                    break;

//                case 2:
//                    ArrayList<History> historyList1 = msg.getData().getParcelableArrayList("historyList1");
//                    draweradapter.setNewData(historyList1);
//                    if (historyList1.size()>0){
//                        emptyContent.setVisibility(View.VISIBLE);
//                    }else {
//                        emptyContent.setVisibility(View.GONE);
//                    }
//                    break;
//                case 3:
//                    draweradapter.setNewData(historyList);
//                    break;
            }

        }
    };

    private void showListView(NoticeBean refreshinfo) {
        //        list = generateTips();
//        noticeAdapter = new NoticeAdapter(R.layout.item_notice, generateTips());
        infoBean = new ArrayList<>();
        infoBean.addAll(refreshinfo.getDs());
        noticeAdapter = new NoticeAdapter(R.layout.item_notice, infoBean);
        rvNotice.setLayoutManager(new LinearLayoutManager(this));
        rvNotice.setAdapter(noticeAdapter);

        noticeRefresh.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {
                if (isLoad){
                    Toast.makeText(NoticeActivity.this,"没有更多数据",Toast.LENGTH_SHORT).show();
                    noticeRefresh.loadMoreComplete();
                }else {
                    int i = Integer.parseInt(yema);
                    yema = (i + 1) + "";
                    msgwhat = 1;
                    getokgo(yema);
                    noticeRefresh.loadMoreComplete();
                }
            }

            @Override
            public void onRefreshing() {

                yema="1";
                msgwhat = 0;
                getokgo(yema);
                noticeRefresh.refreshComplete();
            }
        });
        noticeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getApplicationContext(),""+infoBean.get(position).getContentStr(),Toast.LENGTH_SHORT).show();
                showNormalDialog(infoBean.get(position));
            }
        });
    }

    private void showNormalDialog(NoticeBean.DsBean dsBean){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(NoticeActivity.this);
//        normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle(dsBean.getTitleStr());
        normalDialog.setMessage(dsBean.getContentStr());
        normalDialog.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
//                        finish();
                    }
                });
        // 显示
        normalDialog.show();
    }

    private void getokgo(String pagenum) {


    }



    private ArrayList<String> generateTips() {
        ArrayList<String> tips = new ArrayList<>();
        for (int i = 0; i < 50; i++) {

            tips.add("公告列表"+i);
        }

        return tips;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.back:
                finish();
                break;

        }
    }
}
